<?php

class Resposta{
	
	private $script = null;
	private $_useErrorMessage = false;
	
	public function redirect($confirm, $script = null){
		
		$this->script = $script;
		
		$GLOBALS['connectionDefault']->close($confirm);
		
		if($confirm){
			$this->redirectOperacaoRealizada();
		}else{
			$this->redirectfalhaOperacao();
		}
	}
	
	public function setUseErrorMessage($useErrorMessage){
		
		$this->_useErrorMessage = $useErrorMessage;
	}
	
	private function redirectOperacaoRealizada(){
		
		if($this->script){
			header('Location: '.$this->script);
		}else{
			
			$rootFolder = getRootFolderPath();
   			header(sprintf("Location: %sbasico/confirma.php", $rootFolder));
		}
		
		exit;
	}
	
	private function redirectfalhaOperacao(){
		
		if($this->script){
			$script = $this->script;
		}else{
			$rootFolder = getRootFolderPath();
			$script = sprintf("%sbasico/erro.php", $rootFolder);
		}
		
		if($this->_useErrorMessage){
			
			$logger = Logger::getInstance();
			$arrMessages = $logger->getErros();
			$msg = end($arrMessages);
			$msg = base64_encode($msg);

			$script .= '?p='.$msg;
		}
		
		header('Location: '.$script);
		exit;
	}
}